package cn.edu.nju.software.servlet.administrator;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.nju.software.model.administrator.AdministratorLoginVerifier;

/**
 * Servlet verify the login of administrator.
 */
public class AdministratorLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter writer = response.getWriter();

		request.setCharacterEncoding("UTF-8");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		AdministratorLoginVerifier verifier = new AdministratorLoginVerifier();
		if (verifier.verify(username, password)) {
			getServletContext().setAttribute("administratorName", username);
			response.sendRedirect("/cineplex/administrator/administrator.jsp");
			return;
		} else {
			writer.println("<script>alert('Error! Please check the username and password');"
					+ "window.location.href='administrator/index.jsp';</script>");
		}
	}

}
